package cn.com.eado.crm.m.repository

import androidx.lifecycle.LiveData
import androidx.lifecycle.MediatorLiveData
import cn.com.eado.crm.m.AppExecutors
import cn.com.eado.crm.m.bean.PostCompany
import cn.com.eado.crm.m.db.Company
import cn.com.eado.crm.m.db.CusUser
import cn.com.eado.crm.m.db.EnterpriseInfo
import cn.com.eado.crm.m.db.MyDatabase
import cn.com.eado.crm.m.net.MyRetrofit
import cn.com.eado.crm.m.net.Okhttp3Utils
import cn.com.eado.crm.model.NetworkBoundResource
import cn.com.eado.crm.model.Resource
import cn.com.eado.crm.model.net.ApiResponse

class CompanyRepository {

    fun loadCompany(
        CompanyName: String?,
        GroupName: String?,
        IndustryName: String?,
        LinkName: String?,
        Address: String?,
        AreaID: String?,
        UserName: String?,
        rows: Int,
        page: Int
    ): LiveData<Resource<Company>> {
        return object: NetworkBoundResource<Company, Company>(AppExecutors()){
            override fun shouldLoadFromDb(): Boolean = false

            override fun loadFromDb(): LiveData<Company> = MediatorLiveData<Company>()

            override fun createCall() = MediatorLiveData<ApiResponse<Company>>()

            override fun saveCallResult(item: Company) {}

            override fun shouldFetch(data: Company?) = true

            override fun createCall2(): LiveData<ApiResponse<Company>>? = MyRetrofit.getInstance().api.getCompany(null,null,null,null,null,null,null,rows,page)
        }.asLiveData()
    }

    fun getCusUser(
        CompanyID: String?,
        CompanyName: String?,
        CUserName: String?,
        UserName: String?,
        rows: Int,
        page: Int
    ): LiveData<Resource<CusUser>>{
        return object: NetworkBoundResource<CusUser, CusUser>(AppExecutors()){
            override fun shouldLoadFromDb(): Boolean = false

            override fun loadFromDb(): LiveData<CusUser> = MediatorLiveData<CusUser>()

            override fun createCall() = MediatorLiveData<ApiResponse<CusUser>>()

            override fun saveCallResult(item: CusUser) {}

            override fun shouldFetch(data: CusUser?) = true

            override fun createCall2(): LiveData<ApiResponse<CusUser>>? = MyRetrofit.getInstance().api.getCusUser(null,null,null,null,rows,page)
        }.asLiveData()
    }

    fun saveCompany(userId: Int, t: PostCompany): LiveData<Resource<String>> {
        return object: NetworkBoundResource<String, String>(AppExecutors()){
            override fun shouldLoadFromDb(): Boolean = false

            override fun loadFromDb(): LiveData<String> = MediatorLiveData<String>()

            override fun createCall() = MediatorLiveData<ApiResponse<String>>()

            override fun saveCallResult(item: String) {}

            override fun shouldFetch(data: String?) = true

            override fun createCall2(): LiveData<ApiResponse<String>>? = MyRetrofit.getInstance().api.saveCompany(t)
        }.asLiveData()
    }

}